/* NODE MODULE */
var crypto = require ( 'crypto' );
var DBExpr = require('node-dbi').DBExpr; 
var dataset_query = require ( "liwe/utils" ).dataset_query;

function user ( liwe )
{
	this.liwe = liwe;
	this.dbm = liwe.dbm;
};

// {{{ login ( params, cback )
/**
   .. js:function:: user.login ( params, cback )
  
      params:
      		- ``login``
      		- ``email``
      		- ``pwd``
  
      cback:	cback ( err, user_info )
*/
user.prototype.login = function ( params, cback )
{
	var self = this;
	var select = this.dbm.getSelect ()
		.from ( 'user_login', [ 'uid', 'login', 'passwd', 'email' ] );

	if ( params [ 'login' ] ) select.where ( 'login = ?', params [ 'login' ] );
	if ( params [ 'email' ] ) select.where ( 'email = ?', params [ 'email' ] );

	this.dbm.fetchRow ( select, function ( err, row )
	{
		if ( ! row )
		{
			cback ( null );
			return;
		}

		var md5sum = crypto.createHash ( 'md5' );
		md5sum.update ( params [ 'pwd' ] );

		if ( row [ 'passwd' ] != md5sum.digest ( 'hex' ) )
		{
			self.liwe.err_code = 1;
			self.liwe.err_descr = 'Wrong user or password';
			cback ();
			return;
		}

		self._set_session_data ( row );

		var fields = { 'last_log' : new DBExpr ( 'NOW()' ) };
		var where = [ [ 'uid = ?', row [ 'uid' ] ] ];
		self.dbm.update ( 'user_login', fields, where, function ( err )
		{
			delete row [ 'passwd' ];
			cback ( err, row );
			return;
		} );
	} );
};
// }}}
// {{{ logout ()
/**
   .. js:function:: user.logout ()
  
      cback:	cback ( err, user_info )
*/
user.prototype.logout = function ()
{
	var self = this;

	liwe.session.del ();
	// TODO: stats.logout ()

};
// }}}
// {{{ list ( params, cback )
/**
   .. js:function:: user.list ( params, cback )
  
      params:
      		- ``login``
      		- ``email``
      		- ``enabled`` (0 / 1)
  
      cback:	cback ( err, user_info )
*/
user.prototype.list = function ( params, cback )
{
	var self = this;
	var select = this.dbm.getSelect ()
		.from ( 'user_login', [ 'uid', 'login', 'passwd', 'email', 'created', 'last_log', 'enabled' ] );

	if ( params [ 'login' ] ) select.where ( 'login LIKE ?', '%' + params [ 'login' ] + '%' );
	if ( params [ 'email' ] ) select.where ( 'email = ?', params [ 'email' ] );
	if ( params [ 'enabled' ] !== undefined ) select.where ( 'enabled = ?', ( params [ 'enabled' ] ? 1 : 0 ) );

	dataset_query ( this.dbm, select, params, function ( err, tot_rows, select )
	{
		select.order ( 'login' );
	}, cback );

	
	// select.order ( "login" );

/*
	this.dbm.fetchRow ( select, function ( err, row )
	{
		if ( ! row )
		{
			cback ( null );
			return;
		}

		var md5sum = crypto.createHash ( 'md5' );
		md5sum.update ( params [ 'pwd' ] );

		if ( row [ 'passwd' ] != md5sum.digest ( 'hex' ) )
		{
			self.liwe.err_code = 1;
			self.liwe.err_descr = 'Wrong user or password';
			cback ();
			return;
		}

		self._set_session_data ( row );

		var fields = { 'last_log' : new DBExpr ( 'NOW()' ) };
		var where = [ [ 'uid = ?', row [ 'uid' ] ] ];
		self.dbm.update ( 'user_login', fields, where, function ( err )
		{
			delete row [ 'passwd' ];
			cback ( err, row );
			return;
		} );
	} );
*/
};
// }}}
// {{{ _set_session_data ( row )
user.prototype._set_session_data = function ( row )
{
	this.liwe.session.data [ 'uid' ]   = row [ 'uid' ];
	this.liwe.session.data [ 'login' ] = row [ 'login' ];
	this.liwe.session.data [ 'email' ] = row [ 'email' ];
};
// }}}

module.exports = user;
